//批量调价对话框
function batchPrice(selIndex){
	var strMsg1 = getLanMsg("0ec22222-000600");
	var strMsg2 = getLanMsg("0ec22222-000601");
	var strMsg3 = getLanMsg("0ec22222-000602");
	var strMsg4 = getLanMsg("0ec22222-000603");
	var strMsg5 = getLanMsg("0ec22222-000604");
	var strMsg6 = getLanMsg("0ec22222-000605");
	
	var strMsg7 = getLanMsg("0ec22222-000606");
	var strMsg8 = getLanMsg("0ec22222-000607");
	var strMsg9 = getLanMsg("0ec22222-000608");
	var strMsg10= getLanMsg("0ec22222-000609");
	var strMsg11= getLanMsg("0ec22222-000610");
	var strContent = "";
	strContent = strContent + "<div style='width:400px;margin-left:90px;margin-bottom:10px;'>";
	strContent = strContent + "		<input type='radio' id='percentage' name='Radio' value=''>";
	strContent = strContent + "		<span>" + strMsg2 + "  </span>";
	strContent = strContent + "		<select id='selPercentage' name='' style='width:80px'>"
	strContent = strContent + "          <option  value='1' selected>"+strMsg3+"</option>"										
	strContent = strContent + "			 <option  value='2' >"+strMsg4+"</option>"										
	strContent = strContent + "     </select>";
	strContent = strContent + "		<input type='text' id='update_percentage' name='update_percentage'/>";
	strContent = strContent + "     <span>%</span>";		
	strContent = strContent + "</div>";
	
	strContent = strContent + "<div style='width:400px;margin-left:90px;margin-bottom:10px;'>";
	strContent = strContent + "		<input type='radio' id='numerical' name='Radio' value=''>";
	strContent = strContent + "		<span>" + strMsg5 + "  </span>";
	strContent = strContent + "		<select id='selNumerical' name='' style='width:80px'>"
	strContent = strContent + "          <option  value='1' >"+strMsg3+"</option>"										
	strContent = strContent + "			 <option  value='2' selected>"+strMsg4+"</option>"										
	strContent = strContent + "     </select>";
	strContent = strContent + "		<input type='text' id='update_numerical' name='update_numerical' value='' />";
	strContent = strContent + "</div>";
	
	if(bGHVersion==false){
		strContent = strContent + "<div style='width:400px;margin-left:90px;margin-bottom:10px;'>";
		strContent = strContent + "		<input type='checkbox' id='' name='check_box' value=''>";
		strContent = strContent + "		<span>" + strMsg6 + "  </span>";
		strContent = strContent + "		<input type='text' id='validDate_t' name='validDate_t' class='textfieldnew datepicker  pd_5x2px w_109px' style='float:None;'/>";
		strContent = strContent + "     <span>—</span> ";
		strContent = strContent + "		<input type='text' id='unvalidDate_t' name='unvalidDate_t' class='textfieldnew datepicker  pd_5x2px w_109px' style='float:None;'/>";
		strContent = strContent + "</div>";
		strContent = strContent + "<div id='update_msg' name='update_msg' style='margin-left:30px;color:red'>";
		strContent = strContent + " </div>";
	}
	seajs.use('common/dialog/src/js/dialog',
		function(dialog) {
			dialog.confirm({
				type:"confirm",
				title:strMsg1,
				isHtmlContent:true,
				confirm:function(){
//					var arrSon = document.getElementsByName("checkOrgBox");
//					var row=0;
//					for(i=0;i<arrSon.length;i++) {
//		        		if(arrSon[i].checked) {
//			        		var pk_ctRow=arrSon[i].id;
//			        		row=row+1;
//			        		var pk="pk_"+pk_ctRow;
//			        		$("#"+pk+"").val(pk_ctRow);
//		        		}
//		    		}
//		    		if(row==0){
//		    			$("#update_msg").html(strMsg7);
//						return;
//		    		}
		    		var k=0;
		    		var validDate=$("#validDate_t").val();
					var unvalidDate=$("#unvalidDate_t").val();
		    		var arrRadio = document.getElementsByName("Radio");
		    		var arrCheck_box=document.getElementsByName("check_box");
		    		for(var i=1;i<=arrRadio.length;i++){
		    			if(arrRadio[i-1].checked){
//		    				var validDate=$("#validDate_t").val();
//						    var unvalidDate=$("#unvalidDate_t").val();
		    				if(i==1){
			    				var objPercentage = document.getElementById("update_percentage");
						        var strPercentage = objPercentage.value;
						        var selPer=document.getElementById("selPercentage").value;
						        if(strPercentage!=null&&strPercentage!=''){
							        if(strPercentage<0){
							        	$("#update_msg").html(strMsg8);
							            return;
							        }
						        }else{
						        	$("#update_msg").html(strMsg10);
							        return;
						        }
						        if(bGHVersion==false){
							        if(arrCheck_box[0].checked){
							        	if((validDate==null||validDate=='')&&(unvalidDate==null||unvalidDate=='')){
							        		$("#update_msg").html(strMsg10);
								            return;	
							        	}
								        if((validDate!=null&&validDate!='')&&(unvalidDate!=null&&unvalidDate!='')){
								        	if(validDate>unvalidDate){
								        	   $("#update_msg").html(strMsg9);
								               return;	
								        	}						        	
								        }
								        updatePrice(strPercentage,selPer,validDate,unvalidDate,true,selIndex,1);	
							        }else{
							        	updatePrice(strPercentage,selPer,validDate,unvalidDate,true,selIndex,2);	
							        }
						        }else{
						        	updatePrice(strPercentage,selPer,validDate,unvalidDate,true,selIndex,2);
						        }						       						        					       
		    				}else{
		    					var objNumerical = document.getElementById("update_numerical");
						        var strNumerical = objNumerical.value;
						        var selNum=document.getElementById("selNumerical").value;
						        if(strNumerical!=null&&strNumerical!=''){
							        if(strNumerical<0){
							        	$("#update_msg").html(strMsg8);
							            return;
							        }
						        }else{
						        	$("#update_msg").html(strMsg10);
							        return;
						        }
						        if(bGHVersion==false){
							        if(arrCheck_box[0].checked){
							        	if((validDate==null||validDate=='')&&(unvalidDate==null||unvalidDate=='')){
							        		$("#update_msg").html(strMsg10);
								            return;	
							        	}
								        if((validDate!=null&&validDate!='')&&(unvalidDate!=null&&unvalidDate!='')){
								        	if(validDate>unvalidDate){
								        	   $("#update_msg").html(strMsg9);
								               return;	
								        	}
								        }
								        updatePrice(strNumerical,selNum,validDate,unvalidDate,false,selIndex,1);
							        }else{
							        	updatePrice(strNumerical,selNum,validDate,unvalidDate,false,selIndex,2);	
							        }
						        }else{
						        	updatePrice(strNumerical,selNum,validDate,unvalidDate,false,selIndex,2);
						        }						        					        					      
		    				}
		    				k++;
		    			}
		    		}
		    		if(k==0){
		    			if(arrCheck_box[0].checked){
		    				if((validDate==null||validDate=='')&&(unvalidDate==null||unvalidDate=='')){
				        		$("#update_msg").html(strMsg10);
					            return;	
						    }		    				
		    				if((validDate!=null&&validDate!='')&&(unvalidDate!=null&&unvalidDate!='')){
					        	if(validDate>unvalidDate){
					        	   $("#update_msg").html(strMsg9);
					               return;	
					        	}						        	
						    }
						    updatePrice(0,1,validDate,unvalidDate,false,selIndex,1);	
		    			}else{
		    				$("#update_msg").html(strMsg11);
						    return;
		    			}
//		    			if((validDate!=null&&validDate!='')||(unvalidDate!=null&&unvalidDate!='')){
//		    				if((validDate!=null&&validDate!='')&&(unvalidDate!=null&&unvalidDate!='')){
//						        if(validDate>unvalidDate){
//					        	   $("#update_msg").html(strMsg9);
//					               return;	
//						        }
//						    }
//		    				updatePrice(0,1,validDate,unvalidDate,false,selIndex);
//		    			}
		    		}
		    			
		    		return true;		    		
				},
				content:strContent
			});
		});
}


//按照百分比修改单价
function updatePrice(strObj,selObj,validDate,unvalidDate,isPercentage,selIndex,flag){
	//得到所选择的单价
	var taxPrice=document.getElementsByName("taxPrice");
	var arrSon = document.getElementsByName("checkOrgBox");
	for(i=0;i<arrSon.length;i++) {
		var pk_ctRow=arrSon[i].id;
		if(arrSon[i].checked) {		
    		$("#sel_pk").val(pk_ctRow);
    		doCalPrice(pk_ctRow,strObj,selObj,i,validDate,unvalidDate,isPercentage,arrSon.length,selIndex,flag);	
		}else{
			$("#sel_pk").val(null);
			var validDate_temp=$("#"+pk_ctRow+"_validDate").val();
			var unvalidDate_temp=$("#"+pk_ctRow+"_unvalidDate").val();
			doCalPrice(pk_ctRow,strObj,selObj,i,validDate_temp,unvalidDate_temp,isPercentage,arrSon.length,selIndex,flag);			
		}
	}
	
}

function doCalPrice(pk_ctRow,strObj,selObj,index,validDate,unvalidDate,isPercentage,rowLength,selIndex,flag){
	//计算行号
	var editRow = index;
    //是否上浮
    if(selObj==1){
        document.calpriceform.isUp_cal.value = true;
    }else{
    	document.calpriceform.isUp_cal.value = false;
    }
    //是否是按比例
    if(isPercentage==true){
    	document.calpriceform.isPercentage_cal.value = true;
    }else{
    	document.calpriceform.isPercentage_cal.value = false;   	
    }
    //输入的值
    document.calpriceform.inputNum_cal.value = strObj;
    //单价精度
    var priceScale=document.getElementsByName("priceScale");
    if(priceScale.length > editRow){
    	document.calpriceform.priceScale_cal.value=priceScale[editRow].value;
    }
	//单价
	var taxPrice = document.getElementsByName("taxPrice");
	if(taxPrice.length > editRow){
		document.calpriceform.has_tax_price_cal.value = taxPrice[editRow].value;
	}
	//价格项
	var dataValue = document.getElementsByName("dataValue");
	if(dataValue.length > editRow){	
		document.calpriceform.has_tax_price_cal.value = dataValue[dataValue.length/rowLength*index+selIndex].value;		
	}
	//数量
	var num = document.getElementsByName("num");
	if(num.length > editRow){
		document.calpriceform.amount_cal.value = num[editRow].value;
	}
	//币种
	var pk_currency=document.getElementsByName("pk_currency");
	if(pk_currency.length > editRow){
		document.calpriceform.pk_currency_cal.value=pk_currency[editRow].value;
	}
	//金额
	var money = document.getElementsByName("money");
	if(money.length > editRow){
		document.calpriceform.has_tax_net_price_cal.value = money[editRow].value;
	}
	//生效日期
	var arrValidDate = document.getElementsByName("validDate_row");
	//失效日期
	var arrUnvalidDate = document.getElementsByName("unvalidDate_row");
	if(flag==1){
		if(arrValidDate.length > editRow){
			if($("#sel_pk").val()!=''&&$("#sel_pk").val()!=null){
			   document.calpriceform.validDate_cal.value = validDate;	
			}else{
			   document.calpriceform.validDate_cal.value = arrValidDate[editRow].value;
			}
		}
		if(arrUnvalidDate.length > editRow){
			if($("#sel_pk").val()!=''&&$("#sel_pk").val()!=null){
			   document.calpriceform.unvalidDate_cal.value =unvalidDate;	
			}else{
			   document.calpriceform.unvalidDate_cal.value = arrUnvalidDate[editRow].value;
			}
		}
	}else{
		if(arrValidDate.length > editRow){			
			document.calpriceform.validDate_cal.value = arrValidDate[editRow].value;
		}
		if(arrUnvalidDate.length > editRow){
			document.calpriceform.unvalidDate_cal.value = arrUnvalidDate[editRow].value;
		}
	}
//	if(arrValidDate.length > editRow){
//		if(validDate!=null && validDate!=''){
//			if($("#sel_pk").val()!=''&&$("#sel_pk").val()!=null){
//				document.calpriceform.validDate_cal.value = validDate;				
//			}else{
//				document.calpriceform.validDate_cal.value = arrValidDate[editRow].value;
//			}						
//		}else{
//			document.calpriceform.validDate_cal.value = arrValidDate[editRow].value;
//		}		
//	}
//	//失效日期
//	var arrUnvalidDate = document.getElementsByName("unvalidDate_row");
//	if(arrUnvalidDate.length > editRow){
//		if(unvalidDate!=null && unvalidDate!=''){
//			if($("#sel_pk").val()!=''&&$("#sel_pk").val()!=null){
//				document.calpriceform.unvalidDate_cal.value =unvalidDate;				
//			}else{
//				document.calpriceform.unvalidDate_cal.value = arrUnvalidDate[editRow].value;
//			}						
//		}else{
//			document.calpriceform.unvalidDate_cal.value = arrUnvalidDate[editRow].value;
//		}		
//	}
	
	var formStr = $("#calpriceform").serialize();
	jQuery.ajax( {
		url : ebvp_context+"/contract/calprice",
		type : "POST",
		data : "&" + formStr,
		dataType : "text",
		async : false,
		success : function(result) {
		   if(result!=null){
		   	 var arrResult=eval(result);
		   	 for(var loop = 0; loop < arrResult.length; loop++){
		   	 	var obj = arrResult[loop];
		   	 	var pk1=pk_ctRow+"_"+selIndex;		   	 	
		   	 	var pk2=pk_ctRow+"_validDate";
		   	 	var pk3=pk_ctRow+"_unvalidDate";		   	 	
		   	 	$("#"+pk1+"").next().val(obj.has_tax_price_cal);		   	 	
		   	 	$("#"+pk2+"").next().html(obj.validDate_cal);
		   	 	$("#"+pk2+"").val(obj.validDate_cal);
		   	 	$("#"+pk3+"").next().html(obj.unvalidDate_cal);	
		   	 	$("#"+pk3+"").val(obj.unvalidDate_cal);	   	 	
		   	 }
		   }
		}
	});
		
}	
